//
//  OwnerViewController.m
//  SpiritWalk
//
//  Created by dllo on 16/3/17.
//  Copyright © 2016年 王鑫. All rights reserved.
//

#import "OwnerViewController.h"
#import "HFStretchableTableHeaderView.h"
#import "OwnerTableViewCell.h"

#import "ownerCollectionViewCell.h"
#import "OwnerCell.h"
#import "CoreDataManager.h"

#import "Target+CoreDataProperties.h"
#import "SaveTableViewCell.h"
#import "Tran+CoreDataProperties.h"
#import "BookViewController.h"

@interface OwnerViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)UITableView *ownerTableView;
@property(nonatomic,copy)NSArray *titleArr;
@property (nonatomic,strong)HFStretchableTableHeaderView *stretchHeaderView;
@property(nonatomic,strong)UISegmentedControl *segement;

@property(nonatomic,strong)CoreDataManager *manager;
@property(nonatomic,copy)NSArray *arr;
@property(nonatomic,strong)UITableView *ta;
@property(nonatomic,strong)NSMutableArray *ti;

@property(nonatomic,strong)NSMutableArray *titlea;
@property(nonatomic,strong)UICollectionView *collection;

@property(nonatomic,assign)NSInteger i;

@end

@implementation OwnerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor cyanColor];
    
    self.title=@"我的";

    self.navigationController.navigationBar.translucent=NO;
    [self setUpTableView];
   

    
}
-(void)viewWillAppear:(BOOL)animated{
    self.manager=[CoreDataManager manager];
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Target" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"num=0"];
    [fetchRequest setPredicate:predicate];
    self.arr=[self.manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
    if (fetchedObjects == nil) {
        
    }
    
    self.ti=[NSMutableArray array];
    for (Target *e in fetchedObjects) {
        
        [self.ti addObject:e.title];
        NSLog(@"%@",self.ti);
    }
    
    self.ta=[self.collection viewWithTag:1000];
    
    [self.ta reloadData];
    

    
}

-(void)setUpTableView{
    
    self.ownerTableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-113) style:UITableViewStylePlain];
    
//    self.ownerTableView.rowHeight=HEIGHT-113-200;
    
    self.ownerTableView.delegate=self;
    self.ownerTableView.dataSource=self;
    
    [self.view addSubview:self.ownerTableView];
    
    [self.ownerTableView registerClass:[OwnerTableViewCell class] forCellReuseIdentifier:NSStringFromClass([OwnerTableViewCell class])];
    
    [self.ownerTableView registerClass:[OwnerCell class] forCellReuseIdentifier:NSStringFromClass([OwnerCell class])];
    
    [self initStretchHeader];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView==self.ownerTableView) {
   
    if (indexPath.row==0) {
        
        return 40;
    }
    else{
        
        return HEIGHT-313-40;
    }
    }
    else if(tableView==self.ta || self.ta.tag==1000){
        
        return 40;
    }
    else if(tableView==self.ta || self.ta.tag==1001){
        
        return 100;
    }
    return 0;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView==self.ownerTableView) {
 
    return 2;
    }
    
    else if(tableView.tag==1000){
        
        return self.ti.count;
    }
    else{
        
        return self.titlea.count;
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView==self.ownerTableView) {

    if (indexPath.row==0) {
    OwnerTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([OwnerTableViewCell class])];
        [cell.segement addTarget:self action:@selector(change) forControlEvents:UIControlEventValueChanged];
        self.segement=cell.segement;
        self.segement .selectedSegmentIndex=0;
    return cell;
    }
    
    else{
        
        OwnerCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([OwnerCell class]) forIndexPath:indexPath];
        cell.ownerCollectionView.dataSource=self;
        cell.ownerCollectionView.delegate=self;
        [cell.ownerCollectionView registerClass:[ownerCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([ownerCollectionViewCell class])];
        
        self.collection=cell.ownerCollectionView;
        return cell;
    }
    }
    else if(self.ta.tag==1000) {
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
        cell.textLabel.text=self.ti[indexPath.row];
        
        cell.backgroundColor=[UIColor clearColor];
        cell.opaque=YES;
        return cell;
    }
    else if(self.ta.tag==1001){
        SaveTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SaveTableViewCell class]) forIndexPath:indexPath];
        
        
        
       
        Tran *tran=self.titlea[indexPath.row];
        
        cell.titleLabel.text=tran.title;
       
        cell.backgroundColor=[UIColor clearColor];
        cell.opaque=YES;
        return cell;
    }
    
    
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag==1000) {
        BookViewController *bookVC=[[BookViewController alloc] init];
        
        UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
        if ([cell.textLabel.text isEqualToString:@"我的运动日常"]) {
            bookVC.i=4;
        }
        if ([cell.textLabel.text isEqualToString:@"每日一万步"]) {
            bookVC.i=0;
        }
        if ([cell.textLabel.text isEqualToString:@"每日称重"]) {
            bookVC.i=1;
        }
        if ([cell.textLabel.text isEqualToString:@"多喝水"]) {
            bookVC.i=2;
        }
        if ([cell.textLabel.text isEqualToString:@"健身房报道"]) {
            bookVC.i=3;
        }
        [self presentViewController:bookVC animated:YES completion:^{
            
            
        }];
        
    }
    
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return 2;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item==0) {
   
    ownerCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ownerCollectionViewCell class]) forIndexPath:indexPath];
    cell.ownerCollectionTable.dataSource=self;
        cell.ownerCollectionTable.delegate=self;
    self.ta=cell.ownerCollectionTable;
    [cell.ownerCollectionTable registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
        self.ta.tag=1000;
    
    self.i=indexPath.item;
    return cell;
    }
       else {
           ownerCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ownerCollectionViewCell class]) forIndexPath:indexPath];
           cell.ownerCollectionTable.dataSource=self;
           cell.ownerCollectionTable.delegate=self;
           self.ta=cell.ownerCollectionTable;
           self.ta.tag=1001;
           [cell.ownerCollectionTable registerClass:[SaveTableViewCell class] forCellReuseIdentifier:NSStringFromClass([SaveTableViewCell class])];
           return cell;
            
        }
}




- (void)initStretchHeader
{
    //背景
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300)];
    bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    bgImageView.clipsToBounds = YES;
    bgImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"我的背景" ofType:@"jpg"]];
    
    //背景之上的内容
    UIView *contentView = [[UIView alloc] initWithFrame:bgImageView.bounds];
    contentView.backgroundColor = [UIColor clearColor];
    
    /*
     UIImageView *avater = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
     avater.image = [UIImage imageNamed:@"avater"];
     avater.center = contentView.center;
     [contentView addSubview:avater];
     */
    
    self.stretchHeaderView = [HFStretchableTableHeaderView new];
    [self.stretchHeaderView stretchHeaderForTableView:self.ownerTableView withView:bgImageView subViews:contentView];
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.stretchHeaderView scrollViewDidScroll:scrollView];
}

- (void)viewDidLayoutSubviews
{
    [self.stretchHeaderView resizeView];
}
-(void)change{
    
    if (self.segement.selectedSegmentIndex==0) {
        NSIndexPath *path=[NSIndexPath indexPathForItem:0 inSection:0];
        
        [self.collection scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
        
       
        
        self.manager=[CoreDataManager manager];
        
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Target" inManagedObjectContext:self.manager.managedObjectContext];
        [fetchRequest setEntity:entity];
        
        
        
        // Specify criteria for filtering which objects to fetch
        //谓词
        //包含语句
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"num=0"];
        [fetchRequest setPredicate:predicate];
        self.arr=[self.manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
        
        NSError *error = nil;
        NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
        
        if (fetchedObjects == nil) {
            
        }
        
        self.ti=[NSMutableArray array];
        for (Target *e in fetchedObjects) {
            
            [self.ti addObject:e.title];
            NSLog(@"%@",self.ti);
        }
        
        self.ta=[self.collection viewWithTag:1000];
        
        [self.ta reloadData];

        
        
        
    }
    if (self.segement.selectedSegmentIndex==1) {
        NSIndexPath *path=[NSIndexPath indexPathForItem:1 inSection:0];
        
        [self.collection scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
        
        self.manager=[CoreDataManager manager];
        
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tran" inManagedObjectContext:self.manager.managedObjectContext];
        [fetchRequest setEntity:entity];
        
        
        
        // Specify criteria for filtering which objects to fetch
        //谓词
        //包含语句
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"num=0"];
        [fetchRequest setPredicate:predicate];
        self.arr=[self.manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
        
        NSError *error = nil;
        NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
        
        if (fetchedObjects == nil) {
            
        }
        
        self.titlea=[NSMutableArray array];
        for (Tran *e in fetchedObjects) {
            
            [self.titlea addObject:e];
            NSLog(@"%@",self.titlea);
        }
        
        self.ta=[self.collection viewWithTag:1001];
        
        [self.ta reloadData];
        
        

        
        
        
        
        
        
    }
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView==self.collection) {
       
        self.segement.selectedSegmentIndex=(NSInteger ) scrollView.contentOffset.x/414;
        self.segement.selected=YES;
        
        
        
        
        
    }
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
